package com.shigui.news.controller.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.shigui.news.controller.BaseAction;
import com.shigui.news.dao.NewsDao;
import com.shigui.news.enums.ResponseCode;
import com.shigui.news.exception.UserException;
import com.shigui.news.interceptor.JsonFormat;
import com.shigui.news.model.Comment;
import com.shigui.news.model.News;
import com.shigui.news.model.User;
import com.shigui.news.service.CommentService;
/**
 * 增删改查 curd
 * @author Shiguiwu
 *
 */
@Scope("prototype")
@Controller("commentAction")
public class CommentAction extends BaseAction {

	@Autowired
	private CommentService commentService;
	
	@Autowired
	private NewsDao newsDao;
	
	private News news;
	
	private Comment comment;
	
	/**
	 *进行此操作必须要用户登录
	 */
	
	public void add(){

		
		User userLogin = findByPersistCodeOrFail();
		
		
		if(userLogin==null){
			
			throw new UserException(ResponseCode.USER_ALREADY_LOGOUT);
		}
		
		commentService.insert(comment);
		
		getHttpRequest().setAttribute(JsonFormat.RESPONSE_MESSAGE_KEY, comment.getContent());
		
		getHttpRequest().setAttribute(JsonFormat.RESPONSE_DATE_KEY, comment);
	}
	
	public void delete(){
		
		User userLogin = findByPersistCodeOrFail();
		
		
		if(userLogin==null){
			
			throw new UserException(ResponseCode.USER_ALREADY_LOGOUT);
		}
		Comment comment2 = commentService.selectById(comment.getId());
		
		
		commentService.delete(comment2);
	}
	
	public void select(){
		
		
		
		Comment commen = commentService.selectById(comment.getId());
		
		getHttpRequest().setAttribute(JsonFormat.RESPONSE_DATE_KEY, commen);
	}
	public void getCommentsList(){
		
		List<Comment> comments = commentService.getCommentsList(comment.getNewsId());
		List<Comment> comments2 =new ArrayList<Comment>();
		
		for(Comment commen : comments){
			
			comments2.add(commen);
		}
		
		getHttpRequest().setAttribute(JsonFormat.RESPONSE_DATE_KEY, comments2);
	}
	
	/**
	 * 根据news分页
	 */
	public void getListByNews(){
		
		News ne = newsDao.selectById(news.getNewsId());
		
		setTotal(commentService.countCommentsList(ne));
		
		List<Comment> comments = commentService.select(ne, getPage(), getCount());
		
		List<Comment> data =new ArrayList<Comment>();
		
		for(Comment commen : comments){
			
			data.add(commen);
		}
		/**
		 * 格式化还会结果
		 */
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("page", getPage());
		
		map.put("count", getCount());
		
		map.put("total", getTotal());
		
		map.put("data", data);
		
		getHttpRequest().setAttribute(JsonFormat.RESPONSE_DATE_KEY, map);
	}
	
	public void update(){
		
		User userLogin = findByPersistCodeOrFail();
		
		
		if(userLogin==null){
			
			throw new UserException(ResponseCode.USER_ALREADY_LOGOUT);
		}
		
		commentService.update(comment);
	}

	

	public Comment getComment() {
		return comment;
	}

	public void setComment(Comment comment) {
		this.comment = comment;
	}

	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}

	
	
	
}
